package com.thinkingstar.iads.test.urule.builtIn.action;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.nutz.boot.tools.AsSpringBean;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.bstek.urule.model.library.action.annotation.ActionBean;
import com.bstek.urule.model.library.action.annotation.ActionMethod;
import com.bstek.urule.model.library.action.annotation.ActionMethodParameter;

/**
 * 定义内置动作库
 * @author chi
 *
 */

@IocBean
@AsSpringBean
@ActionBean(name="测试一下")
public class StringAction {

	@Inject 
	Dao dao;
	
	
	@ActionMethod(name="sql查询")
	@ActionMethodParameter(names={"sql语句","Map参数"})
	public List<Map> getMapList(String sqltext,Map map){
		Sql sql = Sqls.create(sqltext);
		if(map!=null){
			Set<Entry<String, Object>> entrySet = map.entrySet();
			for (Entry<String, Object> entry : entrySet) {
				sql.params().set(entry.getKey(), entry.getValue());
			}
		}
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<Map<String,Object>> list = new ArrayList<>();
				while (rs.next()){
					Map<String,Object> map = new HashMap<>();
					ResultSetMetaData metaData = rs.getMetaData();
					int count = metaData.getColumnCount();
					for (int i = count; i > 0; i--) {
						String filed = metaData.getColumnName(i);
						map.put(filed, rs.getString(filed)); 
					}
					list.add(map);
				}
				return list;
			}
		});
		dao.execute(sql);
		List<Map> list = sql.getList(Map.class);
		return list;
	}
	
	/**
	 * 删除、修改、添加
	 */
	@ActionMethod(name="sql操作")
	@ActionMethodParameter(names={"sql语句","Map参数"})
	public int operationSQL(String sqltext,Map map){
		Sql sql = Sqls.create(sqltext);
		if(map!=null){
			Set<Entry<String, Object>> entrySet = map.entrySet();
			for (Entry<String, Object> entry : entrySet) {
				sql.params().set(entry.getKey(), entry.getValue());
			}
		}
		dao.execute(sql);
		return sql.getUpdateCount();
	}
	
	
	/**
	 * 删除、修改、添加
	 */
	@ActionMethod(name="sql批量操作")
	@ActionMethodParameter(names={"sql语句","List参数"})
	public int operationBatchSQL(String sqltext,List<Map> list){
		Sql sql = Sqls.create(sqltext);
		if(list!=null){
			for (Map map : list) {
				Set<Entry<String, Object>> entrySet = map.entrySet();
				for (Entry<String, Object> entry : entrySet) {
					sql.params().set(entry.getKey(), entry.getValue());
				}
				sql.addBatch();
			}
		}
		dao.execute(sql);
		return sql.getUpdateCount();
	}
	

	
	@ActionMethod(name="创建一个Map对象")
	@ActionMethodParameter(names={})
	public  Map createMap(){
		return new HashMap();
	}

	@ActionMethod(name="创建一个List对象")
	@ActionMethodParameter(names={})
	public  List<Map> createList(){
		return new ArrayList<Map>();
	}

	@ActionMethod(name="两个整数相加")
	@ActionMethodParameter(names={"第一个参数","第二个参数"})
	public  int getInteger(String obj1,String obj2){
		return Integer.parseInt(obj1)+Integer.parseInt(obj2);
	}


	@ActionMethod(name="转换double类型")
	@ActionMethodParameter(names={"数值"})
	public  Double getDouble(Object obj){
		return Double.parseDouble(obj.toString());
	}


	@ActionMethod(name="字符串包含比较")
	@ActionMethodParameter(names={"原字符串","包含的字符串"})
	public  boolean getDouble(String str1,String str2){
		return str1.contains(str2);
	}



	@ActionMethod(name="保留N位小数")
	@ActionMethodParameter(names={"原数据","保留的位数"})
	public Double getDouble(Double str1,int str2){
		BigDecimal b = new BigDecimal(str1);
		double f1 = b.setScale(str2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return f1;
	}


	@ActionMethod(name="获取集合中的map对象")
	@ActionMethodParameter(names={"list集合","索引位置"})
	public  Map getMap(List<Map> list,int index){
		return list.get(index);
	}


	@ActionMethod(name="获取List的大小")
	@ActionMethodParameter(names={"list集合"})
	public  int listSize(List<?> list){
		return list.size();
	}

	@ActionMethod(name="清空List")
	@ActionMethodParameter(names={"list集合"})
	public  void listClean(List<?> list){
		list.clear();
	}


	@ActionMethod(name="删除某条List数据")
	@ActionMethodParameter(names={"list集合","索引位置"})
	public  void deletelist(List<?> list,int index){
		if(index<list.size()&&index>=0)
			list.remove(index);
	}


	@ActionMethod(name="输出List数据到控制台")
	@ActionMethodParameter(names={"list集合"})
	public  void showlist( List<Map> list){
		for (Map map : list) {
			Set<Entry<Integer, String>> entrySet = map.entrySet();
			for (Entry<Integer, String> entry : entrySet) {
				System.out.println(entry.getKey()+"-->"+entry.getValue());
			}
			System.out.println("\n");
		}
	}

}
